package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ghost.config.GhostConfig;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_GHOST_GET_UNION_NEWS, desc = "获取商会情报")
public class GhostGetUnionNewsCmd extends AbstractCommand {

    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.GetUnionNewsListReqMsg reqMsg = GhostActivityProto.GetUnionNewsListReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        GhostConfig ghostConfig = GhostMgr.getGhostConfig();
        if (!ActivityMgr.activityInTime(ghostConfig.getActivityInfo())) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostMgr.checkUserData(player);

        if (StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_JOIN_UNION;
            return;
        }

        GhostActivityProto.CrossGhostGetUnionNewsReqMsg.Builder builder = GhostActivityProto.CrossGhostGetUnionNewsReqMsg.newBuilder();
        builder.setActivityId(ghostConfig.getActivityId());
        builder.setServerId(player.getUserInfo().getOriginalServerId());
        builder.setType(type);
        builder.setUnionUid(player.getUserInfo().getUnionUid());
        player.sendPacket(CrossProtocol.C2_GHOST_ACTIVITY_GET_UNION_NEWS, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            GhostActivityProto.GetUnionNewsListRespMsg.Builder builder = GhostActivityProto.GetUnionNewsListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_GHOST_GET_UNION_NEWS, builder);
        }
    }
}
